import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;

import javax.media.opengl.GL;
import javax.media.opengl.glu.GLU;

import com.sun.opengl.impl.mipmap.Image;
import com.sun.opengl.util.texture.Texture;


public class Boat {
	GL gl;
	GLU glu;
	private int shaderprogram;
	private int vert_shader, frag_shader;
	public Vector3D[] units;
	public int num_of_units,option;
	public boolean[] damaged_unit;
	private int damaged_units = 0;
	public boolean killed;
	private boolean rotated = false;
	private Vector3D[] polygon;
	private float[] color;
	private Vector3D[] vertex;
	private float scale,start_time, field = 0;
	Vector3D offset;//just position
	private int polygon_num, vertex_num, board_num;
	private int ship[],radar[],cabin[],gun[];
	private float waving_x = 1.0f;
	private float waving_y = 1.0f;
	private Matrix4x4 matz = new Matrix4x4();
	private Matrix4x4 maty = new Matrix4x4();
    private Matrix4x4 matx = new Matrix4x4();
	private Mipmap mipmap;
	Texture boxTexture1;
	
	public Boat(GL gl_, GLU glu_, int size, Vector3D position){
		gl=gl_;
		glu=glu_;
		ship = new int[1];
		radar = new int[1];
		cabin = new int[1];
		gun = new int[1];
		board_num = size;
		num_of_units = size;
		killed = false;
		units = new Vector3D[board_num];
		damaged_unit = new boolean[board_num]; 
		if(MyJoglCanvas.player_field == 2) field = 160;
		position.y += field;
		for(int i = 0;i < board_num; i++) {
			units[i] = new Vector3D();
			damaged_unit[i] = false;
		}
		if(board_num == 1) {
			units[0].setXYZ(position);
		}
		if(board_num == 2) {
			units[0].setXYZ(position); units[0].y -= 4f;
			units[1].setXYZ(position); units[1].y += 4f;
		}
		if(board_num == 3) {
			units[0].setXYZ(position); units[0].y -= 8f;
			units[1].setXYZ(position); 
			units[2].setXYZ(position); units[2].y += 8f;
		}
		if(board_num == 4) {
			units[0].setXYZ(position); units[0].y -= 12f;
			units[1].setXYZ(position); units[1].y -= 4f;
			units[2].setXYZ(position); units[2].y += 4f;
			units[3].setXYZ(position); units[3].y += 12f;
		}

		offset = new Vector3D(position.x,position.y,position.z);
		mipmap = new Mipmap(gl,glu,4);
		start_time = System.currentTimeMillis()%100000;
		if(gl != null) {
			loadShader();
			CreateTexture(null);
		}
		vertex_init();
		polygon_init();
	}
	public int try_to_kill(Vector3D _target) {
		Vector3D target = new Vector3D(_target.x,_target.y,_target.z);
		target.y += 3f;
		if(killed) return 0;
		for(int i = 0; i < board_num; i++) {
			System.out.println("Target: " + target.x + " " + target.y + "Unit: " + units[i].x + " " + units[i].y);
			if(target.x == units[i].x && target.y == units[i].y) {
				if(damaged_unit[i]) return 0;
				damaged_unit[i] = true;
				damaged_units++;
				if(damaged_units == board_num) {
					killed = true;
					return 2;
				}
				else return 1;
			}
		}
		return 0;
	}
	private void polygon_init() {
		switch(board_num) { 
		case 1 :
			polygon_num = 29;
			make_polygon_1();
			break;
		case 2 :
			polygon_num = 42;
			make_polygon_2();
			break;
		case 3 :
			polygon_num = 52;
			make_polygon_3();
			break;
		case 4 :
			polygon_num = 73;
			make_polygon_4();
			break;
		default :
			System.out.println("Invalid size\n");
			break;
		}
	}
	private void vertex_init() {
		switch(board_num) { 
		case 1 :
			scale = 1.2f;
			vertex_num = 34;
			make_vertex_1();
			break;
		case 2 :
			scale = 1.5f;
			vertex_num = 58;
			make_vertex_2();
			break;
		case 3 :
			scale = 1.5f;
			vertex_num = 74;
			make_vertex_3();
			break;
		case 4 :
			scale = 1.7f;
			vertex_num = 114;
			make_vertex_4();
			break;
		default :
			System.out.println("Invalid size\n");
			break;
		}
	}
/////////////////////////////////////////////////////////////////////
//VERTEX OPERATIONS
///////////////////////////////////////////
	private void make_vertex_gun(float y_offset) {
		vertex[74] = new Vector3D(-0.5f,1.0f+y_offset,0.5f); vertex[75] = new Vector3D(0.5f,1.0f+y_offset,0.5f);
		vertex[76] = new Vector3D(-0.5f,2.0f+y_offset,0.5f); vertex[77] = new Vector3D(0.5f,2.0f+y_offset,0.5f); //lower boxes
	    vertex[78] = new Vector3D(-0.5f,1.0f+y_offset,1.2f); vertex[79] = new Vector3D(0.5f,1.0f+y_offset,1.2f);
		vertex[80] = new Vector3D(-0.5f,2.0f+y_offset,1.2f); vertex[81] = new Vector3D(0.5f,2.0f+y_offset,1.2f);//upper boxes
		
		vertex[82] = new Vector3D(-0.2f,0.0f+y_offset,1.1f); vertex[83] = new Vector3D(-0.1f,0.0f+y_offset,1.1f);
		vertex[84] = new Vector3D(-0.2f,1.0f+y_offset,0.8f); vertex[85] = new Vector3D(-0.1f,1.0f+y_offset,0.8f); //lower boxes
	    vertex[86] = new Vector3D(-0.2f,0.0f+y_offset,1.2f); vertex[87] = new Vector3D(-0.1f,0.0f+y_offset,1.2f);
		vertex[88] = new Vector3D(-0.2f,1.0f+y_offset,0.9f); vertex[89] = new Vector3D(-0.1f,1.0f+y_offset,0.9f);//upper boxes
		
		vertex[90] = new Vector3D(0.1f,0.0f+y_offset,1.1f); vertex[91] = new Vector3D(0.2f,0.0f+y_offset,1.1f);
		vertex[92] = new Vector3D(0.1f,1.0f+y_offset,0.8f); vertex[93] = new Vector3D(0.2f,1.0f+y_offset,0.8f); //lower boxes
	    vertex[94] = new Vector3D(0.1f,0.0f+y_offset,1.2f); vertex[95] = new Vector3D(0.2f,0.0f+y_offset,1.2f);
		vertex[96] = new Vector3D(0.1f,1.0f+y_offset,0.9f); vertex[97] = new Vector3D(0.2f,1.0f+y_offset,0.9f);//upper boxes
	}
	private void make_vertex_antenn(float y_offset) {
		vertex[58] = new Vector3D(-0.5f,1.0f+y_offset,0.5f); vertex[59] = new Vector3D(0.5f,1.0f+y_offset,0.5f);
		vertex[60] = new Vector3D(-0.5f,2.0f+y_offset,0.5f); vertex[61] = new Vector3D(0.5f,2.0f+y_offset,0.5f); //lower boxes
	    vertex[62] = new Vector3D(-0.5f,1.0f+y_offset,1.2f); vertex[63] = new Vector3D(0.5f,1.0f+y_offset,1.2f);
		vertex[64] = new Vector3D(-0.5f,2.0f+y_offset,1.2f); vertex[65] = new Vector3D(0.5f,2.0f+y_offset,1.2f);//upper boxes
		
		vertex[66] = new Vector3D(-0.05f,1.7f+y_offset,1.2f); vertex[67] = new Vector3D(0.05f,1.7f+y_offset,1.2f);
		vertex[68] = new Vector3D(-0.05f,1.8f+y_offset,1.2f); vertex[69] = new Vector3D(0.05f,1.8f+y_offset,1.2f); //lower boxes
	    vertex[70] = new Vector3D(-0.05f,1.7f+y_offset,4.2f); vertex[71] = new Vector3D(0.05f,1.7f+y_offset,4.2f);
		vertex[72] = new Vector3D(-0.05f,1.8f+y_offset,4.2f); vertex[73] = new Vector3D(0.05f,1.8f+y_offset,4.2f);//upper boxes
		
	}
	private void make_vertex_radar(float y_offset) {
		vertex[34] = new Vector3D(-0.5f,1.0f+y_offset,0.5f);
		vertex[35] = new Vector3D(0.5f,1.0f+y_offset,0.5f); vertex[36] = new Vector3D(-0.5f,2.0f+y_offset,0.5f); vertex[37] = new Vector3D(0.5f,2.0f+y_offset,0.5f); //lower boxes
		
	    vertex[38] = new Vector3D(-0.5f,1.0f+y_offset,1.2f); vertex[39] = new Vector3D(0.5f,1.0f+y_offset,1.2f);
		vertex[40] = new Vector3D(-0.5f,2.0f+y_offset,1.2f); vertex[41] = new Vector3D(0.5f,2.0f+y_offset,1.2f);//upper boxes
		
		vertex[42] = new Vector3D(-0.1f,1.5f+y_offset,1.2f); vertex[43] = new Vector3D(0.1f,1.5f+y_offset,1.2f);
		vertex[44] = new Vector3D(-0.1f,1.5f+y_offset,1.3f); vertex[45] = new Vector3D(0.1f,1.5f+y_offset,1.3f);
		vertex[46] = new Vector3D(-0.6f,1.5f+y_offset,1.3f); vertex[47] = new Vector3D(0.6f,1.5f+y_offset,1.3f);
		vertex[48] = new Vector3D(-0.6f,1.5f+y_offset,1.8f); vertex[49] = new Vector3D(0.6f,1.5f+y_offset,1.8f);
		vertex[50] = new Vector3D(-0.7f,1.4f+y_offset,1.4f); vertex[51] = new Vector3D(0.7f,1.4f+y_offset,1.4f);
		vertex[52] = new Vector3D(-0.7f,1.4f+y_offset,1.7f); vertex[53] = new Vector3D(0.7f,1.4f+y_offset,1.7f);
		vertex[54] = new Vector3D(-0.6f,1.5f+y_offset,1.4f); vertex[55] = new Vector3D(0.6f,1.5f+y_offset,1.4f);
		vertex[56] = new Vector3D(-0.6f,1.5f+y_offset,1.7f); vertex[57] = new Vector3D(0.6f,1.5f+y_offset,1.7f);
		
	}
	private void make_vertex_cabin(float y_offset) {
		vertex[20] = new Vector3D(-0.5f,-1.0f+y_offset,0.5f); vertex[21] = new Vector3D(0.5f,-1.0f+y_offset,0.5f); vertex[22] = new Vector3D(-0.5f,1.0f+y_offset,0.5f);
		vertex[23] = new Vector3D(0.5f,1.0f+y_offset,0.5f); vertex[24] = new Vector3D(-0.5f,2.0f+y_offset,0.5f); vertex[25] = new Vector3D(0.5f,2.0f+y_offset,0.5f); //lower boxes
		
		vertex[26] = new Vector3D(-0.5f,-1.0f+y_offset,1.0f); vertex[27] = new Vector3D(0.5f,-1.0f+y_offset,1.0f); vertex[28] = new Vector3D(-0.5f,1.0f+y_offset,1.0f);
		vertex[29] = new Vector3D(0.5f,1.0f+y_offset,1.0f); vertex[30] = new Vector3D(-0.5f,1.0f+y_offset,1.5f); vertex[31] = new Vector3D(0.5f,1.0f+y_offset,1.5f);
		vertex[32] = new Vector3D(-0.5f,2.0f+y_offset,1.5f); vertex[33] = new Vector3D(0.5f,2.0f+y_offset,1.5f);//upper boxes
	}
	private void make_vertex_1() {
		
		vertex = new Vector3D[vertex_num];
		vertex[0] = new Vector3D(0.0f,-5.0f,0.5f); vertex[1] = new Vector3D(-0.5f,-3.0f,0.5f); vertex[2] = new Vector3D(0.5f,-3.0f,0.5f);
		vertex[3] = new Vector3D(-0.8f,-1.0f,0.5f);vertex[4] = new Vector3D(0.8f,-1.0f,0.5f);	vertex[5] = new Vector3D(-1.0f,1.0f,0.5f);
		vertex[6] = new Vector3D(1.0f,1.0f,0.5f);  vertex[7] = new Vector3D(-1.0f,2.0f,0.5f);	vertex[8] = new Vector3D(1.0f,2.0f,0.5f);
		vertex[9] = new Vector3D(0.0f,4.0f,0.5f);//upper field
		
		vertex[10] = new Vector3D(0.0f,-4.0f,-0.5f); vertex[11] = new Vector3D(-0.1f,-3.0f,-0.5f); vertex[12] = new Vector3D(0.1f,-3.0f,-0.5f);
		vertex[13] = new Vector3D(-0.3f,-1.0f,-0.5f);vertex[14] = new Vector3D(0.3f,-1.0f,-0.5f);  vertex[15] = new Vector3D(-0.5f,1.0f,-0.5f);
		vertex[16] = new Vector3D(0.5f,1.0f,-0.5f);	vertex[17] = new Vector3D(-0.5f,2.0f,-0.5f);   vertex[18] = new Vector3D(0.5f,2.0f,-0.5f);
		vertex[19] = new Vector3D(0.0f,4.0f,-0.5f);//lower field
		
		make_vertex_cabin(0.0f);
	}
	private void make_vertex_2() {
		vertex = new Vector3D[vertex_num];
		vertex[0] = new Vector3D(0.0f,-6.0f,0.5f); vertex[1] = new Vector3D(-0.5f,-4.0f,0.5f); vertex[2] = new Vector3D(0.5f,-4.0f,0.5f);
		vertex[3] = new Vector3D(-0.8f,-2.0f,0.5f);vertex[4] = new Vector3D(0.8f,-2.0f,0.5f);	vertex[5] = new Vector3D(-1.0f,0.0f,0.5f);
		vertex[6] = new Vector3D(1.0f,0.0f,0.5f);  vertex[7] = new Vector3D(-1.0f,3.0f,0.5f);	vertex[8] = new Vector3D(1.0f,3.0f,0.5f);
		vertex[9] = new Vector3D(0.0f,6.0f,0.5f);//upper field
		
		vertex[10] = new Vector3D(0.0f,-5.0f,-0.5f); vertex[11] = new Vector3D(-0.1f,-4.0f,-0.5f); vertex[12] = new Vector3D(0.1f,-4.0f,-0.5f);
		vertex[13] = new Vector3D(-0.3f,-2.0f,-0.5f);vertex[14] = new Vector3D(0.3f,-2.0f,-0.5f);  vertex[15] = new Vector3D(-0.5f,0.0f,-0.5f);
		vertex[16] = new Vector3D(0.5f,0.0f,-0.5f);	vertex[17] = new Vector3D(-0.5f,3.0f,-0.5f);   vertex[18] = new Vector3D(0.5f,3.0f,-0.5f);
		vertex[19] = new Vector3D(0.0f,6.0f,-0.5f);//lower field
		
		make_vertex_cabin(-1.0f);
		make_vertex_radar(1.0f);
	}
	private void make_vertex_3() {
		vertex = new Vector3D[vertex_num];
		vertex[0] = new Vector3D(0.0f,-7.0f,0.5f); vertex[1] = new Vector3D(-0.5f,-6.0f,0.5f); vertex[2] = new Vector3D(0.5f,-6.0f,0.5f);
		vertex[3] = new Vector3D(-0.8f,-4.0f,0.5f);vertex[4] = new Vector3D(0.8f,-4.0f,0.5f);	vertex[5] = new Vector3D(-1.0f,-2.0f,0.5f);
		vertex[6] = new Vector3D(1.0f,-2.0f,0.5f);  vertex[7] = new Vector3D(-1.0f,5.0f,0.5f);	vertex[8] = new Vector3D(1.0f,5.0f,0.5f);
		vertex[9] = new Vector3D(0.0f,8.0f,0.5f);//upper field
		
		vertex[10] = new Vector3D(0.0f,-6.0f,-0.5f); vertex[11] = new Vector3D(-0.1f,-6.0f,-0.5f); vertex[12] = new Vector3D(0.1f,-6.0f,-0.5f);
		vertex[13] = new Vector3D(-0.3f,-4.0f,-0.5f);vertex[14] = new Vector3D(0.3f,-4.0f,-0.5f);  vertex[15] = new Vector3D(-0.5f,-2.0f,-0.5f);
		vertex[16] = new Vector3D(0.5f,-2.0f,-0.5f);	vertex[17] = new Vector3D(-0.5f,5.0f,-0.5f);   vertex[18] = new Vector3D(0.5f,5.0f,-0.5f);
		vertex[19] = new Vector3D(0.0f,8.0f,-0.5f);//lower field
		make_vertex_cabin(-2.0f);
		make_vertex_radar(0.0f);
		make_vertex_antenn(1.5f);
	}
	private void make_vertex_4() {
		vertex = new Vector3D[vertex_num];
		vertex[0] = new Vector3D(0.0f,-9.0f,0.5f); vertex[1] = new Vector3D(-0.5f,-8.0f,0.5f); vertex[2] = new Vector3D(0.5f,-8.0f,0.5f);
		vertex[3] = new Vector3D(-0.8f,-6.0f,0.5f);vertex[4] = new Vector3D(0.8f,-6.0f,0.5f);	vertex[5] = new Vector3D(-1.0f,-4.0f,0.5f);
		vertex[6] = new Vector3D(1.0f,-4.0f,0.5f);  vertex[7] = new Vector3D(-1.0f,7.0f,0.5f);	vertex[8] = new Vector3D(1.0f,7.0f,0.5f);
		vertex[9] = new Vector3D(0.0f,10.0f,0.5f);  vertex[110] = new Vector3D(-1.0f,1.0f,0.5f);	vertex[111] = new Vector3D(1.0f,1.0f,0.5f);
		//upper field
		
		vertex[10] = new Vector3D(0.0f,-8.0f,-0.5f); vertex[11] = new Vector3D(-0.1f,-8.0f,-0.5f); vertex[12] = new Vector3D(0.1f,-8.0f,-0.5f);
		vertex[13] = new Vector3D(-0.3f,-6.0f,-0.5f);vertex[14] = new Vector3D(0.3f,-6.0f,-0.5f);  vertex[15] = new Vector3D(-0.5f,-4.0f,-0.5f);
		vertex[16] = new Vector3D(0.5f,-4.0f,-0.5f);	vertex[17] = new Vector3D(-0.5f,7.0f,-0.5f);   vertex[18] = new Vector3D(0.5f,7.0f,-0.5f);
		vertex[19] = new Vector3D(0.0f,10.0f,-0.5f); 	vertex[112] = new Vector3D(-0.5f,1.0f,-0.5f);   vertex[113] = new Vector3D(0.5f,1.0f,-0.5f);//lower field
		make_vertex_cabin(-2.0f);
		make_vertex_radar(0.0f);
		make_vertex_antenn(1.5f);
		make_vertex_gun(-5.5f);
	}
/////////////////////////////////////////////////////////////////////
//POLYGON OPERATIONS
///////////////////////////////////////////
	private int make_polygon_gun(int i,int tex_num) {
		polygon[i].setXYZ(vertex[66+8]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[70+8]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[71+8]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[67+8]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[68+8]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[72+8]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[70+8]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[66+8]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[69+8]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[73+8]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[72+8]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[68+8]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[67+8]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[71+8]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[73+8]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[69+8]);color[i] = tex_num;i++;//high box
		polygon[i].setXYZ(vertex[70+8]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[72+8]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[73+8]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[71+8]);color[i] = tex_num;i++;
		
		polygon[i].setXYZ(vertex[66+16]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[70+16]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[71+16]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[67+16]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[68+16]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[72+16]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[70+16]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[66+16]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[69+16]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[73+16]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[72+16]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[68+16]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[67+16]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[71+16]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[73+16]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[69+16]);color[i] = tex_num;i++;//high box
		polygon[i].setXYZ(vertex[70+16]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[72+16]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[73+16]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[71+16]);color[i] = tex_num;i++;
		
		polygon[i].setXYZ(vertex[66+24]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[70+24]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[71+24]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[67+24]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[68+24]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[72+24]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[70+24]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[66+24]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[69+24]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[73+24]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[72+24]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[68+24]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[67+24]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[71+24]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[73+24]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[69+24]);color[i] = tex_num;i++;//high box
		polygon[i].setXYZ(vertex[70+24]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[72+24]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[73+24]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[71+24]);color[i] = tex_num;i++;
		return i;
	}
	private int make_polygon_antenn(int i,int tex_num) {
		polygon[i].setXYZ(vertex[58]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[62]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[63]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[59]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[60]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[64]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[62]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[58]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[61]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[65]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[64]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[60]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[59]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[63]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[65]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[61]);color[i] = tex_num;i++;//high box
		polygon[i].setXYZ(vertex[62]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[64]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[65]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[63]);color[i] = tex_num;i++;
		
		polygon[i].setXYZ(vertex[66]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[70]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[71]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[67]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[68]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[72]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[70]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[66]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[69]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[73]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[72]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[68]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[67]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[71]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[73]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[69]);color[i] = tex_num;i++;//high box
		polygon[i].setXYZ(vertex[70]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[72]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[73]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[71]);color[i] = tex_num;i++;
		return i;
	}
	private int make_polygon_radar(int i,int tex_num) {
		polygon[i].setXYZ(vertex[34]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[38]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[39]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[35]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[36]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[40]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[38]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[34]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[37]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[41]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[40]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[36]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[35]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[39]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[41]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[37]);color[i] = tex_num;i++;//high box
		polygon[i].setXYZ(vertex[38]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[40]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[41]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[39]);color[i] = tex_num;i++;
		
		polygon[i].setXYZ(vertex[42]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[44]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[45]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[43]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[46]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[48]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[49]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[47]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[50]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[52]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[56]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[54]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[55]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[57]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[53]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[51]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[46]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[50]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[54]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[46]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[56]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[52]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[48]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[48]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[53]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[57]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[49]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[49]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[47]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[55]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[51]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[47]);color[i] = tex_num;i++;
		return i;
	}
	private int make_polygon_cabin(int i,int tex_num) {
		polygon[i].setXYZ(vertex[22]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[28]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[26]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[20]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[21]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[27]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[29]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[23]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[20]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[26]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[27]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[21]);color[i] = tex_num;i++;//low box
		polygon[i].setXYZ(vertex[26]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[28]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[29]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[27]);color[i] = tex_num;i++;
		
		polygon[i].setXYZ(vertex[24]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[32]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[30]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[22]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[23]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[31]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[33]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[25]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[25]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[33]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[32]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[24]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[22]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[30]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[31]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[23]);color[i] = tex_num;i++;//high box
		polygon[i].setXYZ(vertex[30]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[32]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[33]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[31]);color[i] = tex_num;i++;
		return i;
	}
	private int make_simple_ship(int i,int tex_num) {
		polygon[i].setXYZ(vertex[0]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[1]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[2]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[0]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[1]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[3]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[4]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[2]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[3]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[5]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[6]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[4]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[5]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[7]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[8]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[6]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[8]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[7]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[9]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[9]);color[i] = tex_num;i++;//upper field
		
		polygon[i].setXYZ(vertex[11]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[1]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[0]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[10]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[13]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[3]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[1]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[11]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[15]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[5]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[3]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[13]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[17]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[7]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[5]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[15]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[19]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[9]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[7]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[17]);color[i] = tex_num;i++;//left sight
		
		polygon[i].setXYZ(vertex[10]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[0]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[2]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[12]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[12]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[2]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[4]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[14]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[14]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[4]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[6]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[16]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[16]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[6]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[8]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[18]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[18]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[8]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[9]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[19]);color[i] = tex_num;i++;//right sight
		
		polygon[i].setXYZ(vertex[10]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[11]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[12]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[10]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[11]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[13]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[14]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[12]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[13]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[15]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[16]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[14]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[15]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[17]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[18]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[16]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[18]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[19]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[19]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[17]);color[i] = tex_num;i++;//lower field
		return i;
	}
	private int make_extended_ship(int i,int tex_num) {
		polygon[i].setXYZ(vertex[0]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[1]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[2]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[0]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[1]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[3]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[4]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[2]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[3]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[5]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[6]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[4]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[5]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[110]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[111]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[6]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[110]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[7]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[8]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[111]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[8]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[7]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[9]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[9]);color[i] = tex_num;i++;//upper field
		
		polygon[i].setXYZ(vertex[11]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[1]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[0]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[10]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[13]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[3]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[1]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[11]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[15]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[5]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[3]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[13]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[112]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[110]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[5]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[15]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[17]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[7]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[110]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[112]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[19]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[9]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[7]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[17]);color[i] = tex_num;i++;//left sight
		
		polygon[i].setXYZ(vertex[10]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[0]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[2]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[12]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[12]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[2]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[4]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[14]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[14]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[4]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[6]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[16]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[16]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[6]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[111]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[113]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[113]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[111]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[8]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[18]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[18]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[8]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[9]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[19]);color[i] = tex_num;i++;//right sight
		
		polygon[i].setXYZ(vertex[10]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[11]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[12]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[10]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[11]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[13]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[14]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[12]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[13]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[15]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[16]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[14]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[15]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[112]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[113]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[16]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[112]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[17]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[18]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[113]);color[i] = tex_num;i++;
		polygon[i].setXYZ(vertex[18]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[19]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[19]);color[i] = tex_num;i++; polygon[i].setXYZ(vertex[17]);color[i] = tex_num;i++;//lower field

		return i;
	}
	
	private void make_polygon_1() {
		polygon = new Vector3D[polygon_num*4];
		color  = new float[polygon_num*4];
		for(int i = 0; i < polygon_num*4; i++) {
			polygon[i] = new Vector3D();
			
		}
		int i = 0;
		i = make_simple_ship(i,0);
		i = make_polygon_cabin(i,1);
		
	}
	private void make_polygon_2() {
		polygon = new Vector3D[polygon_num*4];
		color  = new float[polygon_num*4];
		for(int i = 0; i < polygon_num*4; i++) {
			polygon[i] = new Vector3D();
			
		}
		int i = 0;
		i = make_simple_ship(i,0);
		i = make_polygon_cabin(i,1);
		i = make_polygon_radar(i,2);
	}
	private void make_polygon_3() {
		polygon = new Vector3D[polygon_num*4];
		color  = new float[polygon_num*4];
		for(int i = 0; i < polygon_num*4; i++) {
			polygon[i] = new Vector3D();
			
		}
		int i = 0;
		i = make_simple_ship(i,0);
		i = make_polygon_cabin(i,1);
		i = make_polygon_radar(i,2);
		i = make_polygon_antenn(i,1);
	}
	private void make_polygon_4() {
		polygon = new Vector3D[polygon_num*4];
		color  = new float[polygon_num*4];
		for(int i = 0; i < polygon_num*4; i++) {
			polygon[i] = new Vector3D();
			
		}
		int i = 0;
		i = make_extended_ship(i,0);
		i = make_polygon_cabin(i,1);
		i = make_polygon_radar(i,2);
		i = make_polygon_antenn(i,1);
		i = make_polygon_gun(i,3);
	}

	private void CreateTexture(Image image)
	 {
		URL imageURL= this.getClass().getResource(
				 "qwerty.png");
		 
		 gl.glGenTextures(1,ship, 0);
		 gl.glBindTexture(GL.GL_TEXTURE_2D, ship[0]);
		 
		 gl.glShadeModel(GL.GL_FLAT);
		 gl.glDepthFunc(GL.GL_LESS);
		 mipmap.makeImages(imageURL, 0);
		 mipmap.LoadImages(0);
		 
		 URL imageURL1= this.getClass().getResource(
				 "qwerty1.png");
			 
		 gl.glGenTextures(1,cabin, 0);
		 gl.glBindTexture(GL.GL_TEXTURE_2D, cabin[0]);
			 
		 gl.glShadeModel(GL.GL_FLAT);
		 gl.glDepthFunc(GL.GL_LESS);
		 mipmap.makeImages(imageURL1, 1);
		 mipmap.LoadImages(1);
		 
		 URL imageURL2= this.getClass().getResource(
				 "qwerty2.png");
		 
		 gl.glGenTextures(1,radar, 0);
		 gl.glBindTexture(GL.GL_TEXTURE_2D, radar[0]);
		 
		 gl.glShadeModel(GL.GL_FLAT);
		 gl.glDepthFunc(GL.GL_LESS);
		 mipmap.makeImages(imageURL2, 2);
		 mipmap.LoadImages(2);
		 
		 URL imageURL3= this.getClass().getResource(
				 "qwerty3.png");
			 
		 gl.glGenTextures(1,gun, 0);
		 gl.glBindTexture(GL.GL_TEXTURE_2D, gun[0]);
			 
		 gl.glShadeModel(GL.GL_FLAT);
		 gl.glDepthFunc(GL.GL_LESS);
		 mipmap.makeImages(imageURL3, 3);
		 mipmap.LoadImages(3);
	 }
	 public void rotate() {
		 if(rotated) {
			 rotated = false;
			 if(board_num == 2) {
				move_to(new Vector3D(-4,-4,0),true);
				units[0].setXYZ(offset); units[0].y -= 4f;
				units[1].setXYZ(offset); units[1].y += 4f; 
			 }
			 if(board_num == 3) {
			 	units[0].setXYZ(offset); units[0].y -= 8f;
			 	units[1].setXYZ(offset); 
			 	units[2].setXYZ(offset); units[2].y += 8f;
		 	 }
			 if(board_num == 4) {
				move_to(new Vector3D(-4,-4,0),true);
		 		units[0].setXYZ(offset); units[0].y -= 12f;
	 			units[1].setXYZ(offset); units[1].y -= 4f;
 				units[2].setXYZ(offset); units[2].y += 4f;
				units[3].setXYZ(offset); units[3].y += 12f;
			 }
		 }
		 else {
			 rotated = true;
			 if(board_num == 2) {
				move_to(new Vector3D(4,4,0),true);
				units[0].setXYZ(offset); units[0].x -= 4f;
				units[1].setXYZ(offset); units[1].x += 4f; 
			 }
			 if(board_num == 3) {
			 	units[0].setXYZ(offset); units[0].x -= 8f;
			 	units[1].setXYZ(offset); 
			 	units[2].setXYZ(offset); units[2].x += 8f;
		 	 }
			 if(board_num == 4) {
				move_to(new Vector3D(4,4,0),true);
		 		units[0].setXYZ(offset); units[0].x -= 12f;
	 			units[1].setXYZ(offset); units[1].x -= 4f;
 				units[2].setXYZ(offset); units[2].x += 4f;
				units[3].setXYZ(offset); units[3].x += 12f;
			 }
		 }
		 if(out_of_field()) rotate();
	 }
	 public boolean in_ships() {
		 for(int i = 0; i < MyJoglCanvas.number_of_editing_boat;i++) {
			 for(int j = 0; j < board_num; j++) {
				 for(int k = 0; k < MyJoglCanvas.boat[i].num_of_units; k++) {
					 if(Math.abs(units[j].x - MyJoglCanvas.boat[i].units[k].x)<16f && Math.abs(units[j].y - MyJoglCanvas.boat[i].units[k].y)<16f) return true;
				 }
			 }
		 }
		 return false;
	 }
	 private boolean out_of_field() {
		 for(int i = 0; i < board_num; i++) {
			 
			 if(units[i].x > 76 || units[i].x < 4 || units[i].y < (4+field) || units[i].y > (76+field)) return true;
		 }
		 return false;
	 }
	 public void move_to(Vector3D off,boolean rotation) {
		 
		 offset.add(off);
		 
		 for(int i = 0; i < board_num; i++) {
			 units[i].add(off);
		 }
		 if(!rotation) {
			 if(out_of_field()) 
				 for(int i = 0; i < board_num; i++) units[i].sub(off);
		 }
	 }
	 public void move_to(Vector3D off) {
		 offset.add(off);
		 for(int i = 0; i < board_num; i++) {
			 units[i].add(off);
			// System.out.println("unit.x before = " + units[i].x + "unit.y before = " + units[i].y + "\n");
		 }
		 if(out_of_field()) { 
			 for(int i = 0; i < board_num; i++) {
				 units[i].sub(off);
				 
			 }
		 offset.sub(off);
		 }
	 }
	 private void loadShader(){
		 vert_shader = gl.glCreateShader(GL.GL_VERTEX_SHADER);
	        frag_shader = gl.glCreateShader(GL.GL_FRAGMENT_SHADER);
	        
	        BufferedReader brv = null;
			try {
				brv = new BufferedReader(
						new FileReader(
								"src/blin.vert"));
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	        String vsrc = "";
	        String line;
	        try {
				while ((line=brv.readLine()) != null) {
				  vsrc += line + "\n";
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        gl.glShaderSource(vert_shader, 1,
	        		new String[] {vsrc}, (int[])null, 0);
	        gl.glCompileShader(vert_shader);
	        
	        BufferedReader brf = null;
			try {
				brf = new BufferedReader(
						new FileReader(
								"src/blin.frag"));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        String fsrc = "";
	        String lines;
	        try {
				while ((lines=brf.readLine()) != null) {
				  fsrc += lines + "\n";
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        gl.glShaderSource(frag_shader, 1,
	        		new String[] {fsrc}, (int[])null, 0);
	        gl.glCompileShader(frag_shader);
	        
	        shaderprogram = gl.glCreateProgram();
	        gl.glAttachShader(shaderprogram, vert_shader);
	        gl.glAttachShader(shaderprogram, frag_shader);
	        gl.glLinkProgram(shaderprogram);
	        gl.glValidateProgram(shaderprogram);
	 }
	 private void setShader(Vector3D pos,boolean edit){
		 	gl.glActiveTexture(GL.GL_TEXTURE0);
		    gl.glBindTexture(GL.GL_TEXTURE_2D,ship[0]);
	        gl.glUniform1i(gl.glGetUniformLocation(
	        		shaderprogram, "map"), 0);
		 	gl.glActiveTexture(GL.GL_TEXTURE1);
		    gl.glBindTexture(GL.GL_TEXTURE_2D,cabin[0]);
	        gl.glUniform1i(gl.glGetUniformLocation(
	        		shaderprogram, "map1"), 1);
		 	gl.glActiveTexture(GL.GL_TEXTURE2);
		    gl.glBindTexture(GL.GL_TEXTURE_2D,radar[0]);
	        gl.glUniform1i(gl.glGetUniformLocation(
	        		shaderprogram, "map2"), 2);
		 	gl.glActiveTexture(GL.GL_TEXTURE3);
		    gl.glBindTexture(GL.GL_TEXTURE_2D,gun[0]);
	        gl.glUniform1i(gl.glGetUniformLocation(
	        		shaderprogram, "map3"), 3);
	        if(edit) {
	        	option = 1;
	        	if(in_ships()) option = 2;
	        }
	        else option = 0;
	        gl.glUniform1i(gl.glGetUniformLocation(
	        		shaderprogram, "option"), option);
//	        gl.glUniform1f(gl.glGetUniformLocation(
//	        		shaderprogram, "time"), (float)(((int)System.currentTimeMillis())%1000000)/10000);
	        
	        gl.glUniform3f(gl.glGetUniformLocation(
	        		shaderprogram, "eyePos"), pos.x,pos.y,pos.z);
	        gl.glUniform3f(gl.glGetUniformLocation(
	        		shaderprogram, "lightPos"), Atmosphere.sun.x,Atmosphere.sun.y,Atmosphere.sun.z);
	        //gl.glPopMatrix();
	 }
	public static Vector3D normal(Vector3D A, Vector3D B, Vector3D C) {
	    float x = A.y()*(B.z() - C.z()) + B.y()*(C.z() - A.z()) + C.y()*(A.z() - B.z());
	    float y = A.z()*(B.x() - C.x()) + B.z()*(C.x() - A.x()) + C.z()*(A.x() - B.x());
	    float z = A.x()*(B.y() - C.y()) + B.x()*(C.y() - A.y()) + C.x()*(A.y() - B.y());
	    Vector3D result = new Vector3D(-x,-y,-z);
	    result.normalize();
	    return result;
	}
	
	private void arbWithMatrix(float x, float y){

		float time=System.currentTimeMillis()%100000 - start_time;

	    float pitch=5*(float)Math.sin((time/1800f*Math.PI + x*y))/180f*(float)Math.PI;
	    matz = Matrix4x4.set((float)Math.cos(Math.PI/2.0f),(float)-Math.sin(Math.PI/2.0f),0,0,
	    		(float)Math.sin(Math.PI/2.0f),(float)Math.cos(Math.PI/2.0f),0,0,
	                   0,0,1,0,
	                   0,0,0,1);
	    maty = Matrix4x4.set((float)Math.cos(pitch),0,(float)Math.sin(pitch),0,
                   0,1,0,0,
                   (float)-Math.sin(pitch),0,(float)Math.cos(pitch),0,
                   0,0,0,1);
    
	    pitch=5*(float)Math.sin((time/900f*Math.PI + x*y))/180f*(float)Math.PI;
	    matx=Matrix4x4.set(1, 0, 0, 0,
	    		0, (float)Math.cos(pitch/5.0f), (float)-Math.sin(pitch/5.0f), 0,
	    		0, (float)Math.sin(pitch/5.0f), (float)Math.cos(pitch/5.0f),0,
	    		0, 0, 0, 1);
	    
	    if(rotated) matx = Matrix4x4.mult(matz, matx);
	    maty=Matrix4x4.mult(matx,maty);
	}
	public void init_targets() {
		if(MyJoglCanvas.current_target < 20) {
			switch(board_num) {
			case 1:
				MyJoglCanvas.list_of_targets[MyJoglCanvas.current_target] = units[0]; MyJoglCanvas.current_target++;
				break;
			case 2:
				MyJoglCanvas.list_of_targets[MyJoglCanvas.current_target] = units[0]; MyJoglCanvas.current_target++;
				MyJoglCanvas.list_of_targets[MyJoglCanvas.current_target] = units[1]; MyJoglCanvas.current_target++;
				break;
			case 3:
				MyJoglCanvas.list_of_targets[MyJoglCanvas.current_target] = units[0]; MyJoglCanvas.current_target++;
				MyJoglCanvas.list_of_targets[MyJoglCanvas.current_target] = units[1]; MyJoglCanvas.current_target++;
				MyJoglCanvas.list_of_targets[MyJoglCanvas.current_target] = units[2]; MyJoglCanvas.current_target++;
				break;
			case 4:
				MyJoglCanvas.list_of_targets[MyJoglCanvas.current_target] = units[0]; MyJoglCanvas.current_target++;
				MyJoglCanvas.list_of_targets[MyJoglCanvas.current_target] = units[1]; MyJoglCanvas.current_target++;
				MyJoglCanvas.list_of_targets[MyJoglCanvas.current_target] = units[2]; MyJoglCanvas.current_target++;
				MyJoglCanvas.list_of_targets[MyJoglCanvas.current_target] = units[3]; MyJoglCanvas.current_target++;
				break;
			}
		}
	}
	public void draw(Camera camera,boolean edit){
		arbWithMatrix(waving_x,waving_y);
		gl.glUseProgram(shaderprogram);
		setShader(Camera.pos,edit);
		gl.glDisable(GL.GL_CULL_FACE);
		gl.glBegin(GL.GL_QUADS);
		Vector3D A = new Vector3D();
		Vector3D B = new Vector3D();
		Vector3D C = new Vector3D();
		Vector3D D = new Vector3D();
		for(int i = 0;i < polygon_num*4; i+=4){
			Vector3D norm = new Vector3D();
			A.setXYZ(Vector3D.mult(polygon[i], scale)); B.setXYZ(Vector3D.mult(polygon[i+1], scale)); C.setXYZ(Vector3D.mult(polygon[i+2], scale)); D.setXYZ(Vector3D.mult(polygon[i+3], scale));
			A=Matrix4x4.mult(maty, A);
			B=Matrix4x4.mult(maty, B);
			C=Matrix4x4.mult(maty, C);
			D=Matrix4x4.mult(maty, D);
			norm = normal(A,B,C);
		//	System.out.println("A.x = " + A.x + "B.x = " + B.x + "C.x = " + C.x +"\n");
			
			gl.glNormal3f(norm.x, norm.y, norm.z);
			gl.glFogCoordf(color[i]);//using just to set some shader attributes
			gl.glTexCoord2d(0,0);
			gl.glVertex3f(A.x+offset.x,A.y+offset.y,A.z+offset.z);
			
			gl.glNormal3f(norm.x, norm.y, norm.z);
			gl.glFogCoordf(color[i+1]);
			gl.glTexCoord2d(1,0);
			gl.glVertex3f(B.x+offset.x,B.y+offset.y,B.z+offset.z);
			
			gl.glNormal3f(norm.x, norm.y, norm.z);
			gl.glFogCoordf(color[i+2]);
			gl.glTexCoord2d(1,1);
			gl.glVertex3f(C.x+offset.x,C.y+offset.y,C.z+offset.z);
			
			gl.glNormal3f(norm.x, norm.y, norm.z);
			gl.glFogCoordf(color[i+3]);
			gl.glTexCoord2d(0,1);
			gl.glVertex3f(D.x+offset.x,D.y+offset.y,D.z+offset.z);
		}

		
		gl.glEnd();
		gl.glEnable(GL.GL_CULL_FACE);
		gl.glUseProgram(0);
	}
	
}	